package com.cdvtc.flower.web.servlet;

import com.cdvtc.flower.dao.OrderDao;
import com.cdvtc.flower.dao.OrderdetailDao;
import com.cdvtc.flower.dao.impl.OrderDaoImpl;
import com.cdvtc.flower.dao.impl.OrderdetailDaoImpl;
import com.cdvtc.flower.model.Order;
import com.cdvtc.flower.model.Orderdetail;

import javax.servlet.http.Part;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@WebServlet(name = "OrderModifyServlet", value = "/OrderModifyServlet")
public class OrderModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // 防止中文文件名乱码

        String order_id = request.getParameter("order_id");
        String product_id = request.getParameter("product_id");
        String customer_id = request.getParameter("customer_id");
        String quantity = request.getParameter("quantity");
        String order_date_time = request.getParameter("order_date_time");
        String total_price = request.getParameter("total_price");
        String review = request.getParameter("review");
        System.out.println(customer_id);
        System.out.println(order_id);
        System.out.println(product_id);
        System.out.println(quantity);
        System.out.println(total_price);

        Order orders = new Order();
        Orderdetail orderdetail = new Orderdetail();

        orders.setCustomer_id(Integer.parseInt(customer_id));
        orders.setOrder_id(Integer.parseInt(String.valueOf(order_id)));
        orderdetail.setOrder_id(Integer.parseInt(String.valueOf(order_id)));
        orderdetail.setProduct_id(Integer.parseInt(product_id));
        orderdetail.setQuantity(Integer.parseInt(quantity));
        orders.setTotal_price(Double.parseDouble(total_price));
        orders.setReview(review);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            orders.setOrder_date_time(df.parse(order_date_time)); // 解析日期字符串为Date对象
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //保存信息至数据库
        OrderDao orderDao = new OrderDaoImpl();
        orderDao.updateOrder(orders);

        OrderdetailDao orderdetailDao = new OrderdetailDaoImpl();
        orderdetailDao.updateOrderdetail(orderdetail);
        //更新当前会话中用的户信息对象
        request.setAttribute("orders", orders);
        // 页面跳转
        request.setAttribute("page", "orderList.jsp");
        request.setAttribute("message", "订单信息修改成功。");
        request.getRequestDispatcher("alert_jump.jsp").forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // 防止中文文件名乱码

        String order_id = request.getParameter("order_id");
        String product_id = request.getParameter("product_id");
        String customer_id = request.getParameter("customer_id");
        String quantity = request.getParameter("quantity");
        String order_date_time = request.getParameter("order_date_time");
        String total_price = request.getParameter("total_price");
        String review = request.getParameter("review");
        System.out.println(customer_id);
        System.out.println(order_id);
        System.out.println(product_id);
        System.out.println(quantity);
        System.out.println(total_price);

        Order orders = new Order();
//        Orderdetail orderdetail = new Orderdetail();

        orders.setCustomer_id(Integer.parseInt(customer_id));
        orders.setOrder_id(Integer.parseInt(String.valueOf(order_id)));
        orders.setProduct_id(Integer.parseInt(product_id));
        orders.setQuantity(Integer.parseInt(quantity));
        orders.setTotal_price(Double.parseDouble(total_price));
        orders.setReview(review);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            orders.setOrder_date_time(df.parse(order_date_time)); // 解析日期字符串为Date对象
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //保存信息至数据库
        OrderDao orderDao = new OrderDaoImpl();
        orderDao.updateOrder(orders);

        OrderdetailDao orderdetailDao = new OrderdetailDaoImpl();
//        orderdetailDao.updateOrderdetail(orderdetail);
        //更新当前会话中用的户信息对象
        request.setAttribute("orders", orders);
        // 页面跳转
        request.setAttribute("page", "OrderList.jsp");
        request.setAttribute("message", "订单信息修改成功。");
        request.getRequestDispatcher("alert_jump.jsp").forward(request, response);
    }
}

